#   Copyright 2013 Stacy Doss stacy.doss@gmail.com
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

package DIME::View::Menu;

sub new {

	my $class  = shift;
	my $parent = shift;
		
	my $T = {
        parent => $parent,
        opts   => {
            check => {
                hlist   => 0,
                browser => 0,
                cli     => 0,
                search  => 0,
                filter  => 0,
                'undef' => 0,
            }
        }
	};
		
	bless $T, $class;

    $T->menu();

	return $T;
}



sub _menu_button {
    my $frame = shift;
    my $text  = shift;    
    return $frame->Menubutton(-text => $text,)->pack(-side => 'left')
}

sub menu_file {
	my $self  = shift;
	my $frame = shift;
	my $app = $self->{parent}->getApplication();
	
	# FILE
	my $mb_file = _menu_button($frame, 'File');
	my $m_file  = $mb_file->Menu(-tearoff => 0);
	
	my $cm_new = $m_file->Menu(-tearoff => 0);
	$cm_new->command(-label => 'Array root', -command => [sub {$app->cmdNew([])}],);
	$cm_new->command(-label => 'Hash root', -command => [sub {$app->cmdNew({})}],);
	$m_file->add('cascade', -label   => 'New', -menu => $cm_new);
	
	$m_file->command(-label => 'Open ...',   -command => [sub {$app->cmdOpen()}],);
	$m_file->command(-label => 'Save',       -command => [sub {$app->cmdSave()}],);
	$m_file->command(-label => 'SaveAs ...', -command => [sub {$app->cmdSaveAs()}],);

	$m_file->separator;

	$m_file->command(-label => 'Save Options', -command => [sub {$app->cmdSaveOptions()}],);

	$m_file->separator;

	$m_file->command(-label => 'TEST', -command => [sub {$app->cmdTest()}],);

	$m_file->separator;

	$m_file->command(-label   => 'Exit',     -command => [sub {$app->cmdExit()}],);

	$mb_file->configure(-menu => $m_file);
}



sub menu_options {
	my $self  = shift;
	my $frame = shift;
	my $app = $self->{parent}->getApplication();

	my $mb_options = _menu_button($frame, 'Options');
	
	my $m_options = $mb_options->Menu(-tearoff => 0,);
    
	$m_options->checkbutton(-label => 'Filters',
					   -variable => \$self->{opts}{check}{filter},
					   -command  => [sub { $app->toggle('filter') }],
					   );

	$m_options->checkbutton(-label => 'Path',
					   -variable => \$self->{opts}{check}{filter},
					   -command  => [sub { $app->toggle('filter') }],
					   );

	$m_options->checkbutton(-label => 'Command Window',
					   -variable => \$self->{opts}{check}{cli},
					   -command  => [sub { $app->toggle('cli') }],
					   );

	$m_options->checkbutton(-label => 'Search Window',
					   -variable => \$self->{opts}{check}{search},
					   -command  => [sub { $app->toggle('search') }],
					   );


	$m_options->checkbutton(-label => 'Column View',
					   -variable => \$self->{opts}{check}{browser},
					   -command  => [sub { $app->toggle('browser') }],
					   );

	$m_options->checkbutton(-label => 'Hierarchical View',
					   -variable => \$self->{opts}{check}{hlist},
					   -command  => [sub { $app->toggle('hlist') }],
					   );

	$m_options->checkbutton(-label => 'Undef Array Elements',
					        -variable => \$self->{opt}{check}{undef},
					        -command  => [sub { $app->toggle('undef') }],
    					   );

	$mb_options->configure(-menu => $m_options);
}



sub menu_tools {
	my $self  = shift;
	my $frame = shift;
	my $app = $self->{parent}->getApplication();

	# TOOLS
	my $mb_tool = _menu_button($frame, 'Tools');
	
	my $m_tool = $mb_tool->Menu(-tearoff => 0,);
	$m_tool->command(-label   => 'Search',   -command => [sub {$app->cmdSearch()}],);

	$mb_tool->configure(-menu => $m_tool);
}



sub menu {

	my $self = shift;
	
	$self->{frame} = $self->{parent}{MW}->Frame(-relief => 'groove', -bd => 1);
	$self->{frame}->pack(-side => 'top', -fill => 'x');
	
	$self->menu_file($self->{frame});
	$self->menu_options($self->{frame});
	$self->menu_tools($self->{frame});
}



1;

__END__